package com.naiterui.ehp.bs.payment.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.naiterui.ehp.bp.constants.PayStatus;
import com.naiterui.ehp.bp.utils.payment.bo.RefundRequestBO;
import com.naiterui.ehp.bs.payment.entity.PayInfo;
import com.naiterui.ehp.bs.payment.entity.RefundInfo;
import com.naiterui.ehp.bs.payment.exception.PayStatusException;
import com.naiterui.ehp.bs.payment.repository.IPayInfoRepository;
import com.naiterui.ehp.bs.payment.repository.IRefundInfoRepository;
import com.naiterui.ehp.bs.payment.service.IRefundInfoService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 退款信息
 *
 * @author 王泽浩
 * @since 1.0.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class RefundInfoServiceImpl implements IRefundInfoService {

    private final IPayInfoRepository payInfoRepository;
    private final IRefundInfoRepository refundInfoRepository;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public RefundInfo getRefundInfo(RefundRequestBO request) {
        log.info("获取退款信息：{}", request);
        String orderSn = request.getOrderSn();
        PayInfo payInfo = this.payInfoRepository.findByOrderSn(orderSn);
        Integer status = payInfo.getStatus();
        // 如果不是支付成功的订单，不能退款
        if (PayStatus.PAY_SUCCESS.getValue() != status) {
            throw new PayStatusException(String.format("订单状态不是支付成功状态，不能退款。订单号：%s 订单状态：%d", orderSn, status));
        }
        //TODO 2019/10/8 11:06 上午 王泽浩 这里判断是否是否全额退款

        return this.refundInfoRepository.save(RefundInfo.builder()
                                                        // 订单号
                                                        .orderSn(orderSn)
                                                        // 待退款
                                                        .status(RefundInfo.ORDER_STATUS_WAIT)
                                                        // 金额
                                                        .price(request.getRefundFee())
                                                        // 退款单号
                                                        .refundOrderSn(request.getRefundOrderSn())
                                                        // 业务类型
                                                        .type(payInfo.getType())
                                                        // 业务通知地址
                                                        .notifyBusinessUrl(request.getNotifyBusinessUrl())
                                                        // 支付流水号
                                                        .tradeSn(payInfo.getTradeSn())
                                                        // 全额退款
                                                        .fullRefund(RefundInfo.FULL_REFUND)
                                                        .createdAt(new Date())
                                                        .createdBy("system")
                                                        .changedAt(new Date())
                                                        .changedBy("system").build());
    }
}
